package com.zwl.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zwl.po.MenuPo;
import com.zwl.po.PageBean;
import com.zwl.po.UserPo;
import com.zwl.service.MenuService;
import com.zwl.service.impl.MenuServiceImpl;
import com.zwl.utils.ConstantUtils;
import com.zwl.utils.StringUtils;

public class MenuServlet extends BaseServlet {
	MenuService menuService=new MenuServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String opr=req.getParameter("opr");
		
		if("loadData".equals(opr)){
			loadData(req, resp);
		}else if("add".equals(opr) || "update".equals(opr)){
			int rows=0;
			String msg="";
			try {
				
				MenuPo saveMenu=getEntity(req);
				
				if("update".equals(opr)){
					rows=menuService.updateMenu(saveMenu);
				}else{
					Integer[] results=menuService.saveMenu(saveMenu);
					rows=results[0];
					int primaryKey=results[1];
					System.out.println("primaryKey:"+primaryKey);
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg="Save failed, Please try again!"+e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('Save successfully!');location.href='menuServlet';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('"+msg+"');history.back();</script>");
			}
			
		}else{
//			List<MenuPo> list=menuService.findByCondition(null);
//			req.setAttribute("list", list);
			Integer pageIndex=StringUtils.str2Integer(req.getParameter("pageIndex"), false, 1);
			PageBean<MenuPo> pageBean=menuService.findPageByCondition(pageIndex, ConstantUtils.PAGE_SIZE, null);
			req.setAttribute("pageBean", pageBean);
			req.getRequestDispatcher("menu_list.jsp").forward(req, resp);
			
		}
	}

	
	
	private MenuPo getEntity(HttpServletRequest req) {
		MenuPo saveMenu=null;
		java.sql.Timestamp currTime=new java.sql.Timestamp(System.currentTimeMillis());
		UserPo currUser=this.getCurrUser(req.getSession());
		
		Integer menuId=StringUtils.str2Integer(req.getParameter("menuId"), false, null);
		if(menuId!=null){
			saveMenu=menuService.findbyId(menuId);
		}else{
			saveMenu=new MenuPo();
			saveMenu.setId(menuId);
			
			
			saveMenu.setCreatedId(currUser.getId());
			saveMenu.setCreatedTime(currTime);
			saveMenu.setMenuIsactive(1);
		}

		saveMenu.setMenuParentId(StringUtils.str2Integer(req.getParameter("menuParentId"), false, null));
		saveMenu.setMenuName(req.getParameter("menuName"));
		saveMenu.setMenuUrl(req.getParameter("menuUrl"));
		saveMenu.setMenuIcon(req.getParameter("menuIcon"));
		saveMenu.setMenuOrder(StringUtils.str2Integer(req.getParameter("menuOrder"), false, null));
		

		saveMenu.setUpdatedId(currUser.getId());
		saveMenu.setUpdatedTime(currTime);
		
		return saveMenu;
	}

	private void loadData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Map<String, Object> paramMap=new HashMap<String, Object>();

		Integer menuId=StringUtils.str2Integer(req.getParameter("menuId"), false, null);
		if(menuId!=null){
			MenuPo menu=menuService.findbyId(menuId);
			req.setAttribute("menu", menu);
			paramMap.put("exincludeMenuId", menuId);
		}

		List<MenuPo> menuList=menuService.findByCondition(paramMap);
		req.setAttribute("menuList", menuList);
		req.getRequestDispatcher("menu_save.jsp").forward(req, resp);
	}
}
